package com.runen.wnhz.runen.ui.adapter.puadapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.runen.wnhz.runen.R;
import com.runen.wnhz.runen.bean.CanDownloadLessonBean;
import com.runen.wnhz.runen.bean.CheckBoxAllDownloadBean;
import com.runen.wnhz.runen.bean.SubmitDownloadBean;
import com.runen.wnhz.runen.common.utils.Base64Utils;
import com.runen.wnhz.runen.ui.activity.main.DownloadActivity;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CanDownloadSmallLessonAdapter extends BaseAdapter {

    private Context context;

    private List<CanDownloadLessonBean.DataBean.NextListBean> next_list;

    private List<CanDownloadLessonBean.DataBean.NextListBean.DurlBean> tvUrlList;

    private int totalSmallSelection;

    private int size;

    public CanDownloadSmallLessonAdapter(Context context, List<CanDownloadLessonBean.DataBean.NextListBean> next_list, int totalSmallSelection) {
        this.context = context;
        this.next_list = next_list;
        tvUrlList = new ArrayList<>();
        this.totalSmallSelection = totalSmallSelection;
        EventBus.getDefault().register(this);
    }

    @Subscriber
    public void onReceiveCheckBoxAllDownloadBeanEvent(CheckBoxAllDownloadBean checkBoxAllDownloadBean) {
        if (checkBoxAllDownloadBean != null) {
            if (!checkBoxAllDownloadBean.isFromAdapter()) {
                if (checkBoxAllDownloadBean.isAllChecked()) {
                    for (CanDownloadLessonBean.DataBean.NextListBean nextListBean : next_list) {
                        nextListBean.setChecked(true);
                    }
                } else {
                    for (CanDownloadLessonBean.DataBean.NextListBean nextListBean : next_list) {
                        nextListBean.setChecked(false);
                    }
                }
                notifyDataSetChanged();
            }
        }
    }

    @Override
    public int getCount() {
        if (next_list != null) {
            return next_list.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.can_download_item_layout, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final CanDownloadLessonBean.DataBean.NextListBean nextListBean = next_list.get(position);
        viewHolder.text_small_selection_name.setText(nextListBean.getName());
        if(nextListBean.isChecked()){
            Glide.with(context).load(R.mipmap.icon_rb_sele).into(viewHolder.image_checked);
        }else{
            Glide.with(context).load(R.mipmap.icon_rb_unsele).into(viewHolder.image_checked);
        }
        viewHolder.linear_small_selection_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<CanDownloadLessonBean.DataBean.NextListBean.DurlBean> durlBeans = nextListBean.getDurl();
                CheckBoxAllDownloadBean checkBoxAllDownloadBean = new CheckBoxAllDownloadBean();
                if (nextListBean.isChecked()) {
                    nextListBean.setChecked(false);
                    Glide.with(context).load(R.mipmap.icon_rb_unsele).into(viewHolder.image_checked);
                    if (DownloadActivity.videoUrl != null && DownloadActivity.videoUrl.size() > 0) {
                        DownloadActivity.videoUrl.remove(position);
                    }
                    DownloadActivity.totalCheckNum = DownloadActivity.totalCheckNum - 1;
                    notifyDataSetChanged();
                } else {
                    nextListBean.setChecked(true);
                    Glide.with(context).load(R.mipmap.icon_rb_sele).into(viewHolder.image_checked);
                    if (durlBeans != null && durlBeans.size() > 0) {
                        SubmitDownloadBean submitDownloadBean = new SubmitDownloadBean();
                        String base64Url = Base64Utils.decode(durlBeans.get(position).getTv_url());
                        submitDownloadBean.setVideoUrl(base64Url.substring(3, base64Url.indexOf("HACHA")));
                        submitDownloadBean.setTitleId(nextListBean.getTitle_id());
                        submitDownloadBean.setNid(nextListBean.getNid());
                        DownloadActivity.videoUrl.add(submitDownloadBean);
                    }
                    DownloadActivity.totalCheckNum = DownloadActivity.totalCheckNum + 1;
                    notifyDataSetChanged();
                }
                if (totalSmallSelection == DownloadActivity.totalCheckNum) {
                    checkBoxAllDownloadBean.setAllChecked(true);
                    DownloadActivity.isFromAdapterCheckAll = false;
                } else {
                    checkBoxAllDownloadBean.setAllChecked(false);
                    DownloadActivity.isFromAdapterCheckAll = true;
                }
                checkBoxAllDownloadBean.setFromAdapter(true);
                EventBus.getDefault().post(checkBoxAllDownloadBean);
            }
        });

        return convertView;
    }

    static class ViewHolder {

        @BindView(R.id.image_checked)
        ImageView image_checked;

        @BindView(R.id.text_small_selection_name)
        TextView text_small_selection_name;

        @BindView(R.id.linear_small_selection_check)
        LinearLayout linear_small_selection_check;

        public ViewHolder(View convertView) {
            ButterKnife.bind(this, convertView);
        }
    }
}
